package com.cz.basetool.ui_work.util.txt;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具类
 * author:Created by ChenZhang on 2016/6/21 0021.
 * function:
 */
public class RegularUtil {
    public static boolean isMobilePhoneNumber(String input){
        boolean check = check(input, "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,2,5-9])|(177))\\d{8}$");
        if(!check){
            check = (input.length() == 11);
        }
        return check;
    }


    public static boolean isTelePhoneNumber(String input){
        boolean check = check(input, "^((\\d{7,8})|(\\d{4})-(\\d{7})|(\\d{3})-(\\d{8})|(\\d{3})-(\\d{8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{4})-(\\d{7})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$");
        return check;
    }

    public static boolean isNumeric(String str){
        return check(str,"[0-9]*");
    }

    public static boolean isEmail(String email){
        return check(email,"^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
    }

    public static boolean check(String input ,String reg){
        Pattern pattern = Pattern.compile(reg);
        Matcher isNum = pattern.matcher(input);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    public static boolean  isIdentificationCard(String cardId){
        boolean check = false;
        String patter18 = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        String patter15 = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$";
        check = check(cardId, patter18);
        if(!check){
            check = check(cardId,patter15);
            if(!check){
                check = (cardId.length() == 18 || cardId.length() == 15);
            }
        }
        return check;
    }
}
